﻿using Lights.Page;
using Lights.Types;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Lights.Controls
{
    /// <summary>
    /// Logica di interazione per SelectionControl.xaml
    /// </summary>
    public partial class SelectionControl : UserControl
    {
        #region Events
        public event SelectionChangedEventHandler SelectedPhotoChanged;
        #endregion

        #region Properties
        public IEnumerable ItemsSource
        {
            get { return (IEnumerable)GetValue(SelectionControl.ItemsSourceProperty); }
            set { SetValue(SelectionControl.ItemsSourceProperty, value); }
        }

        public Photo SelectedItem { get { return this.panel.SelectedItem as Photo; } }
        #endregion

        #region DependencyProperty
        public static readonly DependencyProperty ItemsSourceProperty;
        
        static SelectionControl()
        {
            SelectionControl.ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(SelectionControl));
        }
        #endregion

        #region Fields
        #endregion

        #region Methods
        public SelectionControl()
        {
            InitializeComponent();
        }

        void panel_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.SelectedPhotoChanged != null)
                this.SelectedPhotoChanged(this, e);
        }

        private void cmdAddNewList_Click(object sender, RoutedEventArgs e)
        {
            if (this.panel.Items.Count > 0)
            {
                PhotoList list = new PhotoList("listname");
                foreach (Photo photo in this.ItemsSource)
                {
                    list.Add(photo.Hashcode);
                }
                PhotoList old = Global.CollectionCollector.SingleOrDefault(pl => pl.GetHashCode() == list.GetHashCode());
                if (old != null)
                {
                    if (MessageBox.Show("Esiste già una lista che contiene queste foto!\nVuoi rinominarla?", "Attenzione", MessageBoxButton.YesNo) == MessageBoxResult.No)
                    {
                        return;
                    }
                    Global.PhotoCollector.Remove(old.GetHashCode());
                    list = old;
                }
                MyDialog dialog = new MyDialog("Nome della lista", list.Name);
                if (dialog.ShowDialog().GetValueOrDefault(false) == true)
                {
                    list.Name = dialog.InputText;
                }
                Global.CollectionCollector.Add(list);
            }
        }
        #endregion
    }
}
